package com.atbun.eduservice.service.impl;

import com.atbun.eduservice.entity.EduChapter;
import com.atbun.eduservice.entity.EduVideo;
import com.atbun.eduservice.entity.vo.ChapterVo;
import com.atbun.eduservice.entity.vo.VideoVo;
import com.atbun.eduservice.mapper.EduChapterMapper;
import com.atbun.eduservice.service.EduChapterService;
import com.atbun.eduservice.service.EduVideoService;
import com.atbun.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 章节 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-06
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 根据课程id获取章节信息
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> nestedList(String courseId) {
//        最终返回的数据
        List<ChapterVo> chapterVoList = new ArrayList<>();

//        获取章节信息
        QueryWrapper<EduChapter> qw = new QueryWrapper<>();
        qw.eq("course_id",courseId);
        qw.orderByAsc("sort","id");
        List<EduChapter> eduChapters = this.baseMapper.selectList(qw);

//        获得课时信息
        QueryWrapper<EduVideo> qw2 = new QueryWrapper<>();
        qw2.eq("course_id", courseId);
        qw2.orderByAsc("sort", "id");
        List<EduVideo> eduVideos = this.eduVideoService.list(qw2);

//        填充章节vo数据
        int count1 = eduChapters.size();
        for (int i = 0;i<count1; i++){
            EduChapter eduChapter = eduChapters.get(i);

//            创建章节vo对象
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            chapterVoList.add(chapterVo);

//            封装章节中的小节
            List<VideoVo> videoVoList = new ArrayList<>();
            int count2 = eduVideos.size();
            for (int j =0;j <count2; j++){
                EduVideo eduVideo = eduVideos.get(j);
//                如果章节的id与小节中的章节id相同
                if (eduChapter.getId().equals(eduVideo.getChapterId())){
//                    创建课时vo对象
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }

            }
            chapterVo.setChildren(videoVoList);
        }
        return chapterVoList;
    }

    /**
     * 根据id删除章节信息
     * 删除的时候，需要判断该章节是否有小节，如果有小节，需要删除小节后，再删除章节
     * @param id
     * @return
     */
    @Override
    public boolean removeChapterById(String id) {
        if (eduVideoService.getCountByChapterId(id)){
            throw new GuliException(20001,"该分章节下存在视频课程，请先删除视频课程");
        }
        Integer result = this.baseMapper.deleteById(id);

        return null != result && result > 0;
    }

    /**
     * 根据课程id删除章节小节
     * @param courseId
     * @return
     */
    @Override
    public boolean deleteChapterByCourseId(String courseId) {
        Integer deleteChapter = this.baseMapper.deleteChapterByCourseId(courseId);
        return null != deleteChapter && deleteChapter > 0;
    }
}
